package com.dayaokeji.rhythmschoolstudent.client.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.mine.account.StudentIdLoginActivity;
import com.dayaokeji.rhythmschoolstudent.databases.dao.UserInfoDao;
import com.dayaokeji.rhythmschoolstudent.service.AppStateService;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.ServerResponseCode;
import com.dayaokeji.server_api.api.UserApi;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 系统设置
 */
public class SystemSettingActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_clear_cache)
    LinearLayout llClearCache;

    private static final UserApi userApi = ApiUtils.getApi(UserApi.class);
    private Call<ServerResponse<Void>> logoutCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_setting);
        setSupportActionBar(toolbar);
    }

    @OnClick({
            R.id.tv_qiut,
            R.id.ll_clear_cache,
            R.id.tv_exit
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_qiut:
                logout();
                break;
            case R.id.ll_clear_cache:
                clearCache();
                break;
            case R.id.tv_exit:

                break;
        }
    }

    /**
     * 清理，功能凑数
     */
    private void clearCache() {
        if (llClearCache != null) {
            llClearCache.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.success("清理成功");
                }
            },2000L);
        }
    }


    /**
     * 退出
     */
    private void logout() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("您确认要退出当前账号吗？").setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                logoutHandle();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        }).create();
        alertDialog.show();
        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.warring_red));
        alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.colorPrimary));
    }

    /**
     * 退出登录网络请求处理
     */
    private void logoutHandle() {
        logoutCall = userApi.logout();
        logoutCall.enqueue(new ServerCallback<Void>(this,"正在退出...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    UserUtils.setUserInfo(null);
                    UserUtils.setAccessToken(null);
                    UserInfoDao.delUserInfo();
                    stopService(new Intent(SystemSettingActivity.this, AppStateService.class));
                    ActivityStackManager.finishAll();
                    Intent intent = new Intent(SystemSettingActivity.this, StudentIdLoginActivity.class);
                    startActivity(intent);
                    ActivityStackManager.finish();
                }else{
                    if (response.getHeader() != null) {
                        int code = response.getHeader().getCode();
                        if (ServerResponseCode.INVALID_TOKEN == code) {
                            ToastUtils.error("退出失败，无效Token");
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (logoutCall != null) {
            logoutCall.cancel();
        }
        super.onDestroy();
    }
}
